package com.meiqiongproperty.yiguanjia.commom;

import android.app.Activity;
import android.app.Application;
import android.content.Context;

import com.google.gson.GsonBuilder;
import com.meiqiongproperty.yiguanjia.BuildConfig;
import com.meiqiongproperty.yiguanjia.http.CodeVerify;
import com.meiqiongproperty.yiguanjia.http.HttpManager;
import com.meiqiongproperty.yiguanjia.http.RetrofitParams;
import com.meiqiongproperty.yiguanjia.http.interceptor.ParamsInterceptor;
import com.meiqiongproperty.yiguanjia.utils.image.CApplicationInitUtil;

import java.util.ArrayList;

import okhttp3.Interceptor;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * application
 *
 * @author Chen
 * @version V1.0
 * @package com.meiqiongproperty.yiguanjia.commom
 * @date 17/1/13
 */
public class YApplication extends Application {
    private static YApplication app;
    private static Context context;
    private Activity currentActivity;

    public static Context getContext() {
        return context;
    }

    public YApplication() {
        app = this;
    }

    public Activity getCurrentActivity() {
        return currentActivity;
    }

    public void setCurrentActivity(Activity currentActivity) {
        this.currentActivity = currentActivity;
    }

    public static YApplication getInstance() {
        if (app == null) {
            app = new YApplication();
        }
        return app;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        ArrayList<Interceptor> interceptors = new ArrayList<>();
        CApplicationInitUtil.initImageLoader(getApplicationContext());
        interceptors.add(new ParamsInterceptor());
        RetrofitParams params = new RetrofitParams();
        // data 转换器
        GsonBuilder builder = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss");
        params.setConverterFactory(GsonConverterFactory.create(builder.create()));
        params.setConnectTimeoutSeconds(10);
        params.setReadTimeoutSeconds(10);
        params.setWriteTimeoutSeconds(10);
        params.setInterceptors(interceptors);
        HttpManager.getInstance().create(BuildConfig.API_URL, new CodeVerify(), params);

    }
}
